package zuoye_3_17;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Zuoye_1 {
	 public static void main(String[] args) {
	        ArrayList<Student> students = new ArrayList<>();
	        Scanner scanner = new Scanner(System.in);

	        for (int i = 0; i < 3; i++) {
	            while (true) {
	                try {
	                    System.out.println("请输入第" + (i + 1) + "个学生的姓名：");
	                    String name = scanner.next();
	                    if (name.length() < 2 || name.length() > 10) {
	                        throw new Exception("姓名长度必须在2-10之间");
	                    }

	                    System.out.println("请输入第" + (i + 1) + "个学生的年龄：");
	                    int age = scanner.nextInt();
	                    if (age < 18 || age > 40) {
	                        throw new Exception("年龄必须在18-40岁之间");
	                    }

	                    students.add(new Student(name, age));
	                    break;
	                } catch (InputMismatchException e) {
	                    System.out.println("输入的年龄必须是数字");
	                    scanner.next(); // 清空错误的输入
	                } catch (Exception e) {
	                    System.out.println(e.getMessage());
	                }
	            }
	        }

	        System.out.println(students);
	    }
}
